package com.yancy.dao.impl;

import com.yancy.dao.NewDao;
import com.yancy.entity.New;
import com.yancy.util.DataSourceUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewDaoImpl implements NewDao {
    @Override
    public List<New> getNew() {
        List<New> list = new ArrayList<>();
        String sql = "select * from new";
        try(Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql)){
            try(ResultSet rs = st.executeQuery()){
                while (rs.next()){
                    New t = new New(rs.getString("id"), rs.getString("title"),rs.getString("content"),  rs.getTimestamp("time"), rs.getString("type"));
                    list.add(t);
                }
            }
        }catch (SQLException e){
            System.out.println(e);
        }

        return list;
    }

    @Override
    public boolean delete(String id) {
        /*根据id删除*/
        String sql = "delete  from new where id =?";
        try(Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql)){
            st.setString(1,id);
            int res = st.executeUpdate();
            if(res==1){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return false;
    }

    @Override
    public boolean update(New updateNew) {
        /*根据传来的updateNew对象更新数据库数据*/
        String sql = "UPDATE new SET title=?,content=?,type =? where id =?";
        try(Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql)){
            st.setString(1,updateNew.getTitle());
            st.setString(2,updateNew.getContent());
            st.setString(3,updateNew.getType());
            st.setString(4,updateNew.getId());
            int res = st.executeUpdate();
            if(res==1){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return false;
    }

    @Override
    public boolean add(New addNew) {
        String sql = "insert into new(id,title,content,type) values(?,?,?,?)";
        try {
            Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1,addNew.getId());
            st.setString(2,addNew.getTitle());
            st.setString(3,addNew.getContent());
            st.setString(4,addNew.getType());
            if(st.executeUpdate()==1){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            System.out.println("AddException:"+e);
        }
        return false;
    }

    @Override
    public New getSingleNew(String id) {
        String sql = "select * from new where id = ?";
        try{
            New singleNew = null;
            Connection conn = DataSourceUtils.getConnection();
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,id);
            try(ResultSet rs = preparedStatement.executeQuery()){
                while (rs.next()){
                    singleNew = new New(rs.getString("id"),rs.getString("title"),rs.getString("content"),  rs.getTimestamp("time"), rs.getString("type"));
                }
            }
            return singleNew;
        }catch (Exception e){
            System.out.println("getSingleNewException: "+e);
        }
        return null;
    }
}
